/**********************************************************************/
/*                                                                    */
/*                                                                    */
/*            You should have received a copy of the GNU General      */
/*            Public License along with gpsim; see the file           */
/*            COPYING.txt. If not, write to the Free Software         */
/*            Foundation, 59 Temple Place - Suite 330,                */
/*            Boston, MA 02111-1307, USA.                             */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/**********************************************************************/

#include "osa.h"
#include "OSAcfg.h"
#include "drv_uart_new.h"
#include "service/osa_bsem.h"
#include "service/osa_qmsg.h"
#include "my_osa_init.h"

#include <stdio.h>
#include <string.h>

extern OST_MSG   rs_msg_rx, rs_msg_tx;
extern OST_QMSG   RS_qmsg_rx;
extern OST_QMSG   RS_qmsg_tx;
OST_FLAG8   RS_event;




unsigned int    TX_ByteNumber = 0;
unsigned int    RX_ByteNumber = 0;
unsigned int 	TX_len;
unsigned int 	RX_len;


void RS_Drv(void)
{  
	
	while(1)
	{
		if (OS_CheckBSem(RS_RCV_MSG)) {
			OS_ResetBSem(RS_RCV_MSG);
			OS_WaitQMsg (RS_qmsg_rx, rs_msg_rx);
		}
		if OS_CheckBSem(RS_NEW_MSG){
			OS_ResetBSem(RS_NEW_MSG);
			OS_WaitQMsg (RS_qmsg_tx, rs_msg_tx);	
			TX_len = strlen(rs_msg_tx);
			if (TX_len > 0) {// FIXME necessario?
			
				if (IEC0bits.U1TXIE == 0) {
					U1STAbits.UTXEN = 1;
					while(U1STAbits.UTXBF);
					U1TXREG = (unsigned char) rs_msg_tx[TX_ByteNumber++];
					IEC0bits.U1TXIE = 1;
				}//(IEC0bits.U1TXIE == 0) 
			}//if (tx_len > 0) 
		}//if OS_CheckQMsg(RS_qmsg_tx)
		OS_Yield();
	}//while
}//void rs_drv

